package com.applozic.mobicomkit.uiwidgets.conversation.messageUtils;

import android.content.Context;

import com.applozic.mobicomkit.contact.AppContactService;
import com.lottery.ruok.lottrey.eventbusObject.BubbleChange;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import property.SharePreferenceProperties;

/**
 * 未读信息管理者
 */
public class BubbleUtils {
    private static final long FILTER_TIME = 1000;
    public static String BUBBLE_KEY = "bubble_key";
    /**
     * 未读信息数存储者
     */
    public static Map<String, Integer> map = new HashMap();
    public static Map<String, Long> mapFilter = new HashMap();
    /**
     * 当前聊天对象id
     */
    public static String userId;
    private static AppContactService appContactService;

    public static String getUserId() {
        return userId;
    }

    public static void setUserId(String userId) {
        BubbleUtils.userId = userId;
        clearByKey(userId);
    }

    public static void add(String key, int count) {
//        if (!fiter(key)) {
//            return;
//        }
//        if (userId != null && userId.equals(key)) {
//            //正在和当前userId聊天，不需要气泡
//            return;
//        }
//        if (map.containsKey(key)) {
//            map.put(key, map.get(key) + count);
//            SharePreferenceProperties.setObj(BUBBLE_KEY, map);
//        } else {
//            map.put(key, count);
//            SharePreferenceProperties.setObj(BUBBLE_KEY, map);
//        }
        EventBus.getDefault().post(new BubbleChange(key));
    }

    /**
     * @param key
     * @return
     */
    private static boolean fiter(String key) {
        if (mapFilter.containsKey(key)) {
            //判断是否太快，太快认为是一条信息
            Long time = mapFilter.get(key);
            long offerTime = System.currentTimeMillis() - time;
            mapFilter.put(key, time);
            return offerTime >= FILTER_TIME;
        } else {
            mapFilter.put(key, System.currentTimeMillis());
        }
        return true;
    }

    public static int get(String key) {
//        if (map.containsKey(key)) {
//            return map.get(key);
//        }
//        return 0;
        return appContactService.getContactById(key).getUnreadCount();
    }

    public static void clearByKey(String key) {
//        map.put(key, 0);
//        SharePreferenceProperties.setObj(BUBBLE_KEY, map);
        EventBus.getDefault().post(new BubbleChange(key));
    }

    /**
     * 获得总气泡数
     *
     * @return
     */
    public static int getTotalNum() {
//        int total = 0;
//        if (map != null) {
//            Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
//            while (iterator.hasNext()) {
//                Map.Entry<String, Integer> next = iterator.next();
//                int num = next.getValue();
//                total += num;
//            }
//        }
//        return total;
        return appContactService.getAllUnReadCount();
    }

    public static void initData(Context context) {
        appContactService = new AppContactService(context);
//        if (!Utils.isEmpty(UserInfo.getUsername())) {
//            BUBBLE_KEY = UserInfo.getUsername() + "bubble_key";
//        }
//        map = SharePreferenceProperties.getObj(BUBBLE_KEY, HashMap.class);
//        if (map == null) {
//            map = new HashMap<>();
//        }
    }

    public static void clearAll() {
        if (map != null) {
            map.clear();
        }
        SharePreferenceProperties.set(BUBBLE_KEY, "");
    }

    public static String getKey() {
        if (map != null) {
            Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Integer> next = iterator.next();
                int num = next.getValue();
                if (num > 0) {
                    return next.getKey();
                }
            }
        }
        return null;
    }
}


